#include "pch.h"
#include "giftibhisttask.h"
#include "util/dbutil.h"

GiftIBHistTask::GiftIBHistTask()
	:CMysqlTask("GiftIBHistTask")
{
}

GiftIBHistTask::~GiftIBHistTask()
{
}

void GiftIBHistTask::SetData(const KKSG::SaveIbGiftRecordArg& oData)
{
	m_oData = oData;
}

void GiftIBHistTask::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
{
	SetMysqlError(MYSQL_ERR_FAILED);
	if (m_oData.opertype()== KKSG::SaveIbGift_All)
	{		
		std::stringstream ss;
		std::string strSelfData = m_oData.giftself().SerializeAsString();
		if(-1 == poMysqlConn->EscapeString(strSelfData.c_str(), (INT32)strSelfData.size(), pcBuffer, nLength))
			return;
		ss << "replace into giftrecord(`_id`, `giftself`, `gifttarget`) values(" << m_oData.roleid() << ", '" << pcBuffer  << "'";

		std::string strTargetData = m_oData.gifttarget().SerializeAsString();
		if(-1 == poMysqlConn->EscapeString(strTargetData.c_str(), (INT32)strTargetData.size(), pcBuffer, nLength))
			return;
		ss << ", '" << pcBuffer <<  "')";
		if(!poMysqlConn->Query(ss.str().c_str(), ss.str().size(), NULL))
		{
			GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
			return;
		}
		SetMysqlError(MYSQL_ERR_SUCESS);
	}
}

void GiftIBHistTask::OnReply()
{
	if(m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		LogError("Write giftrecord data task failed, roleid: %llu, errMsg: %s", m_oData.roleid(), GetErrStream().str().c_str());
	}
}

GiftIBHistTask2::GiftIBHistTask2()
	:DBTaskBase("GiftIBHistTask")
{
}

GiftIBHistTask2::~GiftIBHistTask2()
{
}

void GiftIBHistTask2::SetData(const KKSG::SaveIbGiftRecordArg& oData)
{
	m_oData = oData;
}

// void GiftIBHistTask2::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
// {
// 	SetMysqlError(MYSQL_ERR_FAILED);
// 	if (m_oData.opertype() == KKSG::SaveIbGift_All)
// 	{
// 		std::stringstream ss;
// 		std::string strSelfData = m_oData.giftself().SerializeAsString();
// 		if (-1 == poMysqlConn->EscapeString(strSelfData.c_str(), (INT32)strSelfData.size(), pcBuffer, nLength))
// 			return;
// 		ss << "replace into giftrecord(`_id`, `giftself`, `gifttarget`) values(" << m_oData.roleid() << ", '" << pcBuffer << "'";
// 
// 		std::string strTargetData = m_oData.gifttarget().SerializeAsString();
// 		if (-1 == poMysqlConn->EscapeString(strTargetData.c_str(), (INT32)strTargetData.size(), pcBuffer, nLength))
// 			return;
// 		ss << ", '" << pcBuffer << "')";
// 		if (!poMysqlConn->Query(ss.str().c_str(), ss.str().size(), NULL))
// 		{
// 			GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
// 			return;
// 		}
// 		SetMysqlError(MYSQL_ERR_SUCESS);
// 	}
// }

// void GiftIBHistTask2::OnReply()
// {
// 	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
// 	{
// 		LogError("Write giftrecord data task failed, roleid: %llu, errMsg: %s", m_oData.roleid(), GetErrStream().str().c_str());
// 	}
// }

bool GiftIBHistTask2::db_thread_process()
{
	char* pcBuffer = pdbi_->GetEscapStringBuff();
	SetMysqlError(MYSQL_ERR_FAILED);
	if (m_oData.opertype() == KKSG::SaveIbGift_All)
	{
		std::stringstream ss;
		std::string strSelfData = m_oData.giftself().SerializeAsString();
		if (-1 == pdbi_->EscapeString(strSelfData.c_str(), (INT32)strSelfData.size(), pcBuffer, 0))
			return false;
		ss << "replace into giftrecord(`_id`, `giftself`, `gifttarget`) values(" << m_oData.roleid() << ", '" << pcBuffer << "'";

		std::string strTargetData = m_oData.gifttarget().SerializeAsString();
		if (-1 == pdbi_->EscapeString(strTargetData.c_str(), (INT32)strTargetData.size(), pcBuffer, 0))
			return false;
		ss << ", '" << pcBuffer << "')";
		if (!pdbi_->Query(ss.str().c_str(), ss.str().size(), NULL))
		{
			GetErrStream() << "ErrDesc: " << pdbi_->GetError() << ", ErrNo: " << pdbi_->GetErrno();
			return false;
		}
		SetMysqlError(MYSQL_ERR_SUCESS);
	}
	return false;
}

thread::TPTask::TPTaskState GiftIBHistTask2::presentMainThread()
{
	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		LogError("Write giftrecord data task failed, roleid: %llu, errMsg: %s", m_oData.roleid(), GetErrStream().str().c_str());
	}
	return DBTaskBase::presentMainThread()/*thread::TPTask::TPTASK_STATE_COMPLETED*/;
}